package com.ali.juejin.ui.fragment

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.ali.juejin.R
import com.ali.juejin.adapter.HomeAdapter
import com.ali.juejin.base.BaseFragment
import com.ali.juejin.bean.home.CategoryBean
import com.ali.juejin.model.main.CategoryModel
import com.ali.juejin.ui.activity.CategoryActivity
import com.ali.juejin.ui.fragment.home.CategoryFragment
import com.ali.juejin.ui.fragment.home.FollowFragment
import com.ali.juejin.ui.fragment.home.HotListFragment
import com.ali.juejin.ui.fragment.home.RecommendFragment
import com.blankj.utilcode.util.LogUtils
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import kotlinx.android.synthetic.main.fragment_home.*

class HomeFragment: BaseFragment() {

    val categoryModel by lazy { ViewModelProvider(activity!!).get(CategoryModel::class.java) }
    private val originCategory = ArrayList<CategoryBean>()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return View.inflate(context,R.layout.fragment_home,null)
    }

    override fun initView() {


        home_go_category.setOnClickListener {
            startActivity(Intent(context,CategoryActivity::class.java))
        }
    }

    override fun initData() {
        categoryModel.categoryList.observe(this, Observer {
            value->
            // 配置fragment
            val FollowFragmentCategory = CategoryBean("关注",FollowFragment(),null)
            val RecommendFragmentCategory = CategoryBean("推荐",RecommendFragment(),null)
            val HotListFragmentCategory = CategoryBean("热榜",HotListFragment(),null)

            originCategory.add(FollowFragmentCategory)
            originCategory.add(RecommendFragmentCategory)
            originCategory.add(HotListFragmentCategory)
            value?.let {
                value.forEach {
                        item->
                    originCategory.add(CategoryBean(item.category.category_name,CategoryFragment(),item.category.category_id))

                }
                createViewPager(originCategory)
            }

        })
    }

    fun createViewPager(viewPagerFragments:List<CategoryBean>){
        home_viewpager.offscreenPageLimit = viewPagerFragments.size
        home_viewpager.adapter = HomeAdapter(this,viewPagerFragments.map { item->item.fragment }.toTypedArray())
        home_viewpager.offscreenPageLimit = 3
        TabLayoutMediator(home_tab_layout,home_viewpager,object :TabLayoutMediator.TabConfigurationStrategy{
            override fun onConfigureTab(tab: TabLayout.Tab, position: Int) {
                tab.text = viewPagerFragments[position].title
            }

        }).attach()
        // 默认选中推荐
        home_tab_layout.getTabAt(1)?.select()
    }

}